//
//  XClockInEditViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XClockInEditViewController.h"
#import "RSSingleLineCell.h"
#import "RSSingleLineEditCell.h"

@interface XClockInEditViewController ()


//@property(nonatomic,strong)MyRelativeLayout* navbar;
//
///// title
//@property (nonatomic,weak)UILabel* titleLb;
//
///// x
//@property (nonatomic,weak)UIButton* closeBtn;


@property(nonatomic,strong) MyLinearLayout *contentLayout;

/// 名称
@property (nonatomic,weak)RSSingleLineEditCell* nameCell ;

/// 开始时间
@property (nonatomic,weak)RSSingleLineCell* beginCell ;

@end

@implementation XClockInEditViewController

//- (XClockInModel*)model{
//    if(!_model){
//        _model = [XClockInModel new];
//        _model.beginDate = [NSDate date];
//    }
//    return _model;
//}


- (void)loadView{
    [super loadView];
    // 线性布局
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.backgroundColor = COLOR_BACKGEOUND;
    self.view = rootLayout;
    
//    MyRelativeLayout* navbar = [MyRelativeLayout new];
//    navbar.myHeight = IPHONE_NAVIGATION_BAR_HEIGHT;
//    navbar.myMargin = 0;
//    navbar.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_QUITE];
//    [rootLayout addSubview:navbar];
//    _navbar = navbar;
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.padding = UIEdgeInsetsZero;
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    if(self.operation == EditOperation_Edit){
        [self updateViews];
    }else{
        self.beginCell.subtitle = [[NSDate date] stringYearMonthDay];
    }
}

- (void)setupNavBar{
    
    NSString* title;
    if(self.operation == EditOperation_Edit){
        title = @"修改打卡";
    }else{
        title = @"新增打卡";
    }
    self.navigationItem.title = title;

//    UIBarButtonItem* closeItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(confimClicked:)];
//    self.navigationItem.rightBarButtonItem = closeItem;
    
}

- (void)setupViews{
    
    WeakSelf
    RSSingleLineEditCell* nameCell = [RSSingleLineEditCell cellWithTitle:@"名称"];
    nameCell.placehodler = @"请输入名称";
    nameCell.textChangedBlock = ^(NSString * _Nullable string) {
        weakSelf.model.name = string;
    };
    nameCell.myHorzMargin = 0;
    nameCell.myTop = 20;
    [self.contentLayout addSubview:nameCell];
    _nameCell = nameCell;;
    
    RSSingleLineCell* beginCell = [RSSingleLineCell cellWithTitle:@"开始时间"];
    beginCell.myHorzMargin = 0;
    beginCell.myTop = 12;
    beginCell.showSeparator = NO;
    beginCell.clickable = NO;
    beginCell.subtitleColor = COLOR_THEME_A;
    [self.contentLayout addSubview:beginCell];
    _beginCell = beginCell;
    
    UIButton* confimBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confimBtn.backgroundColor = COLOR_THEME_A;
    [confimBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confimBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [confimBtn addTarget:self action:@selector(confimClicked:) forControlEvents:UIControlEventTouchUpInside];
    confimBtn.titleLabel.font = FONT_FIT(14);
    confimBtn.layer.cornerRadius = PX(4);
    confimBtn.layer.masksToBounds = YES;
    confimBtn.myHeight = PX(40);
    confimBtn.myHorzMargin = 16;
    confimBtn.myTop = 32;
    [self.contentLayout addSubview:confimBtn];
}

- (void)updateViews{
    
    self.nameCell.text = self.model.name;
    
    self.beginCell.subtitle = [self.model.beginDate stringYearMonthDay];
}

#pragma mark  --- action
///关闭
- (void)closeClicked:(UIButton*)sender{
    [self closeViewControllerAnimated:YES];
}

///确定
- (void)confimClicked:(UIButton*)sender{
    
    NSString* name = self.nameCell.text;
    name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(!name.length){
        [Toast toast:@"请输入名称"];
        return;;
    }
    if(self.operation == EditOperation_Add){

        
        XClockInModel* model = [XClockInModel new];
        model.beginDate = [NSDate date];
        model.name = name;
        [[RLMRealm defaultRealm] beginWriteTransaction];
        [[RLMRealm defaultRealm] addObject:model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        if(self.editBlock){
            self.editBlock(model, self.operation);
        }
    }else if (self.operation == EditOperation_Edit){
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.model.name = name;
        [[RLMRealm defaultRealm] addOrUpdateObject:self.model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        if(self.editBlock){
            self.editBlock(self.model, self.operation);
        }
    }

    [self closeViewControllerAnimated:YES];
}
@end
